package com.eaoa.uc.busorder.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.eaoa.base.EABaseAction;
import com.eaoa.constants.PubPermissionConstants;
import com.eaoa.query.bus.BusOrderQuery;
import com.eaoa.uc.busorder.form.BusOrderCondition;
import com.eaoa.uc.busorder.form.BusOrderSearchForm;
import com.stony.core.util.CoreUtils;

public class BusOrderManageSearchAction extends EABaseAction{

	protected ActionForward doWork(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		BusOrderSearchForm sform = (BusOrderSearchForm) form;
		sform.initConditionPost();
		BusOrderCondition condition = (BusOrderCondition) sform.getCondition();
		
		BusOrderQuery query = new BusOrderQuery();
		
		if(hasPermission(request, PubPermissionConstants.PERMISSION_PUBORDER_QUERYALL)){
			//query all
		}else if(hasPermission(request, PubPermissionConstants.PUBORDER_QUERYCUSTOMERS)){
			query.setCustomerno(getEmployee(request).getCustomerids());
		}else{
			query.setCreatorno(getEmployee(request).getAccountno());
		}
		
		query.setBillcode(condition.getBillcode());
		query.setOrdertype(condition.getOrdertype());
		query.setIsTransfer(condition.getIstransfer());
		query.setBillstatus(condition.getBillstatus());
		
		query.setFromaddressname(condition.getFromaddressname());
		query.setToaddressname(condition.getToaddressname());
		query.setToaddressno(CoreUtils.parseLong(condition.getToaddressno()));
		query.setTostorebillcode(condition.getTostorebillcode());
		
		//CUSOTMER
		query.setFetchPubCustomer();
		query.setCustomercode(condition.getCustomercode());
		query.setCustomername(condition.getCustomername());
		
		query.setIslock("N");
		query.setLazy(true);
		query.setPageRange(sform.getPageinfo());
		query.setOrder(condition.getSorttype(), condition.getSortfield());
		query.execute();
		
		request.setAttribute("results", query.getResults());
		
		return mapping.findForward("success");
	}
}
